package sort;

/**
 * 计数排序
 */
public class CountingSort {

    public static void main(String args[]) {

        int nums[] = new int[]{1, 4, 6, 8, 3, 4, 3, 7, 4, 6, 8};
        //输入
        System.out.println("/n 输入 /n");
        printIntArray(nums);

        int c[] = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

        int b[] = new int[nums.length];


        for (int i = 0; i < nums.length; i++) {
            c[nums[i]] = c[nums[i]] + 1;
        }
        for (int i = 1; i < c.length; i++) {
            c[i] = c[i] + c[i - 1];
        }
        //减1：是因为0是第一位
        for (int i = 0; i < nums.length; i++) {
            b[c[nums[i]]-1]=nums[i];
            c[nums[i]]=  c[nums[i]]-1;
        }

        //输出数组
        System.out.println("/n 输出 /n");
        printIntArray(b);
    }
    //打印
    public static void printIntArray(int a[]){
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+"--");
        }
    }

}
